package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.ScoreLine;
import com.yunji.news.mapper.ScoreLineMapper;
import com.yunji.news.service.ScoreLineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 分数线服务实现类
 *
 * @author yunji
 * @since 2024-11-25
 */
@Service
@Slf4j
public class ScoreLineServiceImpl extends ServiceImpl<ScoreLineMapper, ScoreLine> implements ScoreLineService {

    @Override
    public Page<ScoreLine> getScoreLinePage(Page<ScoreLine> page, Long schoolId, Long majorId, 
                                            Integer year, String batch, String subjectType) {
        LambdaQueryWrapper<ScoreLine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(schoolId != null, ScoreLine::getSchoolId, schoolId)
                .eq(majorId != null, ScoreLine::getMajorId, majorId)
                .eq(year != null, ScoreLine::getYear, year)
                .eq(StringUtils.hasText(batch), ScoreLine::getBatch, batch)
                .eq(StringUtils.hasText(subjectType), ScoreLine::getSubjectType, subjectType)
                .eq(ScoreLine::getDeleted, 0)
                .orderByDesc(ScoreLine::getYear)
                .orderByDesc(ScoreLine::getMinScore);

        return this.page(page, wrapper);
    }

    @Override
    public List<ScoreLine> getScoreLinesBySchoolId(Long schoolId) {
        return baseMapper.selectBySchoolId(schoolId);
    }

    @Override
    public List<ScoreLine> getScoreLinesByMajorId(Long majorId) {
        return baseMapper.selectByMajorId(majorId);
    }

    @Override
    public List<ScoreLine> getRecentScoreLines(Integer years) {
        return baseMapper.selectRecentYears(years);
    }

    @Override
    public List<ScoreLine> getScoreLinesByYear(Integer year) {
        return baseMapper.selectByYear(year);
    }

    @Override
    public List<Integer> getYears() {
        return baseMapper.selectYears();
    }

    @Override
    public List<String> getBatches() {
        return baseMapper.selectBatches();
    }

    @Override
    public List<String> getSubjectTypes() {
        return baseMapper.selectSubjectTypes();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createScoreLine(ScoreLine scoreLine) {
        // 设置默认值
        if (scoreLine.getStatus() == null) {
            scoreLine.setStatus(1);
        }
        
        // 计算平均分
        if (scoreLine.getMinScore() != null && scoreLine.getMaxScore() != null) {
            scoreLine.setAvgScore((scoreLine.getMinScore() + scoreLine.getMaxScore()) / 2.0);
        }

        return this.save(scoreLine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateScoreLine(ScoreLine scoreLine) {
        // 重新计算平均分
        if (scoreLine.getMinScore() != null && scoreLine.getMaxScore() != null) {
            scoreLine.setAvgScore((scoreLine.getMinScore() + scoreLine.getMaxScore()) / 2.0);
        }

        return this.updateById(scoreLine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteScoreLine(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteScoreLineBatch(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchImportScoreLines(List<ScoreLine> scoreLines) {
        try {
            for (ScoreLine scoreLine : scoreLines) {
                // 设置默认值
                if (scoreLine.getStatus() == null) {
                    scoreLine.setStatus(1);
                }
                
                // 计算平均分
                if (scoreLine.getMinScore() != null && scoreLine.getMaxScore() != null) {
                    scoreLine.setAvgScore((scoreLine.getMinScore() + scoreLine.getMaxScore()) / 2.0);
                }
            }
            
            return this.saveBatch(scoreLines);
        } catch (Exception e) {
            log.error("批量导入分数线数据失败", e);
            throw new RuntimeException("批量导入失败：" + e.getMessage());
        }
    }
}